<?php

/**
 * 通过配置文件
 * @author zhong <qingflyer@gmail.com>
 */
class ApiYspConfig {

    //图片上传文件配置
    static public $image = array(
        'savePath' => '/tmp',
        'maxSize' => 2097152,
        'override' => 1,
    );
    //分布文件配置
    static public $fastdfs = array(
        'host' => '127.0.0.1',
        'port' => 22122,
    );
    //Mongo 数据库配置
    static public $mongo_config = array(
        'server' => 'mongodb://127.0.0.1:27017',
        'database' => 'dMmrijiDB',
        'user' => 'root',
        'password' => '123456',
    );

    /**
      +--------------------------------------------------------------------------
     * 相册分类，每个用户固定分类名
      +--------------------------------------------------------------------------
     */
    static public $photo_category = array(
        0 => array(
            'title' => '全家福',
            'cover' => 'http://user.mmriji.com/upload/photos/cover.jpg',
            'photoNum' => 0,
            'remark' => '',
            'size' => 0
        ),
        1 => array(
            'title' => '孕期',
            'cover' => 'http://user.mmriji.com/upload/photos/cover.jpg',
            'photoNum' => 0,
            'remark' => '',
            'size' => 0
        ),
        2 => array(
            'title' => '0-1岁',
            'cover' => 'http://user.mmriji.com/upload/photos/cover.jpg',
            'photoNum' => 0,
            'remark' => '',
            'size' => 0
        ),
        3 => array(
            'title' => '1-2岁',
            'cover' => 'http://user.mmriji.com/upload/photos/cover.jpg',
            'photoNum' => 0,
            'remark' => '',
            'size' => 0
        ),
        4 => array(
            'title' => '2-3岁',
            'cover' => 'http://user.mmriji.com/upload/photos/cover.jpg',
            'photoNum' => 0,
            'remark' => '',
            'size' => 0
        ),
        5 => array(
            'title' => '3-4岁',
            'cover' => 'http://user.mmriji.com/upload/photos/cover.jpg',
            'photoNum' => 0,
            'remark' => '',
            'size' => 0
        ),
        6 => array(
            'title' => '4-5岁',
            'cover' => 'http://user.mmriji.com/upload/photos/cover.jpg',
            'photoNum' => 0,
            'remark' => '',
            'size' => 0
        ),
        7 => array(
            'title' => '5-6岁',
            'cover' => 'http://user.mmriji.com/upload/photos/cover.jpg',
            'photoNum' => 0,
            'remark' => '',
            'size' => 0
        ),
        8 => array(
            'title' => '6岁以后',
            'cover' => 'http://user.mmriji.com/upload/photos/cover.jpg',
            'photoNum' => 0,
            'remark' => '',
            'size' => 0
        )
    );
    
    /**
      +--------------------------------------------------------------------------
     * 经验和问答举报类别
      +--------------------------------------------------------------------------
     */
    static public $blog_report = array(
        1 => '含有黄赌毒等违反国家相关法律法规的内容',
        2 => '内容完全与母婴行业没有任何关系',
        3 => '含有污秽言辞',
        4 => '辞不达意，不明白写的是什么，完全没有任何参考价值'
    );
    
    /**
      +--------------------------------------------------------------------------
     * 照片举报类别
      +--------------------------------------------------------------------------
     */
    static public $photo_report = array(
        1 => '含有黄赌毒等违反国家相关法律法规的内容',
        2 => '照片完全与宝宝无关'
    );

    /**
      +--------------------------------------------------------------------------
     * 获取用户的相册分类
      +--------------------------------------------------------------------------
     * @param resource $conn_user 用户数据库连接资源对象
     * @param int $user_id 用户ID
     * @param int $current_id 如果大于-1，则说明只需要指定返回某一个分类相关的信息即可
     * @return array 二维数组，用户自己设置的视频分类信息
      +--------------------------------------------------------------------------
     */
    static public function getUserPhotoCategory($conn_user, $user_id, $current_id = -1) {
        $user_info = $conn_user->getOneRow(self::getTableUser($user_id), 'FphotoCategory', "WHERE FuserId=$user_id");
        if (!empty($user_info['FphotoCategory'])) {
            $photo_category = json_decode($user_info['FphotoCategory']);
            $user_photo_cateogry = BaseYsp::getArray($photo_category);
        } else {
            $user_photo_cateogry = self::$photo_category;
        }

        //解码JSON数据
        foreach ($user_photo_cateogry as $key => $ls) {
            $user_photo_cateogry[$key]['title'] = urldecode($ls['title']);
            $user_photo_cateogry[$key]['remark'] = urldecode($ls['remark']);
        }

        if ($current_id == -1) {
            return $user_photo_cateogry;
        } else {
            return $user_photo_cateogry[$current_id];
        }
    }

    /**
      +--------------------------------------------------------------------------
     * 将相册分类中的中文编码并保存
      +--------------------------------------------------------------------------
     * @param resource $conn_user 用户数据库连接资源对象
     * @param int $user_id 用户ID
     * @param string $photo_category 用户相册分类数组
     * @return boolean 成功返回TRUE,失败返回FALSE
      +--------------------------------------------------------------------------
     */
    static public function saveUserPhotoCategory($conn_user, $user_id, $photo_category) {
        foreach ($photo_category as $key => $ls) {
            $photo_category[$key]['title'] = urlencode($ls['title']);
            $photo_category[$key]['remark'] = urlencode($ls['remark']);
        }
        $data = array(
            'FphotoCategory' => json_encode($photo_category)
        );
        $rs = $conn_user->Update(self::getTableUser($user_id), $data, "WHERE FuserId=$user_id");
        if ($rs !== false) {
            return true;
        } else {
            return false;
        }
    }

    /**
      +--------------------------------------------------------------------------
     * 信息发布及用户相关权限设置
      +--------------------------------------------------------------------------
     * 经验，提问，回答，评论，照片，视频，收藏，关注
     * 用户表中如果设置了，则以用户表中的设置为准，否则以此为准。
     * 用户表中会记录用户每天的发布行为，也和此做对比进行限制。
     */
    static public $user_write_level = array(
        'blogNum' => 10,
        'questionNum' => 10,
        'answerNum' => 100,
        'commentNum' => 100,
        'photoNum' => 50,
        'mediaNum' => 5,
        'collectionNum' => 50,
        'attentionNum' => 500
    );

    /**
      +--------------------------------------------------------------------------
     * 获取用户的发布权限
      +--------------------------------------------------------------------------
     * @param resource $conn_user 用户数据库连接资源对象
     * @param int $user_id 用户ID
     * @param string $category_tag 分类标识符：经验=blog，提问=question，照片=photo，视频=media，收藏=collection，关注=attention
     * @param int $total_num 要更新的总数量，比如照片，存在一次性更新多张的情况
     * @return array 二维数组，状态的error为true说明当天已没有发布权限，无论什么情况都会返回当前编码过的要写入的当天权限数据
      +--------------------------------------------------------------------------
     */
    static public function getUserWriteLevel($conn_user, $user_id, $category_tag, $total_num = 1) {
        $user_info = $conn_user->getOneRow(self::getTableUser($user_id), 'FuserWriteLevel,FuserWriteToday', "WHERE FuserId=$user_id");
        if (!empty($user_info['FuserWriteLevel'])) {
            $write_level = json_decode($user_info['FuserWriteLevel']);
            $user_write_level = BaseYsp::getArray($write_level);
        } else {
            $user_write_level = self::$user_write_level;
        }
        $today_int = strtotime(date('Y-m-d 00:00:00', time()));
        if (!empty($user_info['FuserWriteToday'])) {
            $write_level = json_decode($user_info['FuserWriteToday']);
            $user_write_today = BaseYsp::getArray($write_level);
            if (!isset($user_write_today['blogToday']) || $user_write_today['blogToday'] < $today_int) {
                $user_write_today['blogNum'] = 0;
                $user_write_today['blogToday'] = $today_int;
            }
            if (!isset($user_write_today['questionToday']) || $user_write_today['questionToday'] < $today_int) {
                $user_write_today['questionNum'] = 0;
                $user_write_today['questionToday'] = $today_int;
            }
            if (!isset($user_write_today['photoToday']) || $user_write_today['photoToday'] < $today_int) {
                $user_write_today['photoNum'] = 0;
                $user_write_today['photoToday'] = $today_int;
            }
            if (!isset($user_write_today['mediaToday']) || $user_write_today['mediaToday'] < $today_int) {
                $user_write_today['mediaNum'] = 0;
                $user_write_today['mediaToday'] = $today_int;
            }
            if (!isset($user_write_today['collectionToday']) || $user_write_today['collectionToday'] < $today_int) {
                $user_write_today['collectionNum'] = 0;
                $user_write_today['collectionToday'] = $today_int;
            }
            if (!isset($user_write_today['attentionToday']) || $user_write_today['attentionToday'] < $today_int) {
                $user_write_today['attentionNum'] = 0;
                $user_write_today['attentionToday'] = $today_int;
            }
        } else {
            $user_write_today = array(
                'blogNum' => 0,
                'blogToday' => $today_int,
                'questionNum' => 0,
                'questionToday' => $today_int,
                'photoNum' => 0,
                'photoToday' => $today_int,
                'mediaNum' => 0,
                'mediaToday' => $today_int,
                'collectionNum' => 0,
                'collectionToday' => $today_int,
                'attentionNum' => 0,
                'attentionToday' => $today_int
            );
        }

        //根据当前请求的内容分类判断该分类的内容还有没有发布权限
        $num_tag = $category_tag . 'Num';
        $return_array['status']['error'] = false;
        $return_array['status']['todayNum'] = $user_write_today[$num_tag];
        $return_array['status']['levelNum'] = $user_write_level[$num_tag];
        if ($user_write_today[$num_tag] >= $user_write_level[$num_tag]) {
            $return_array['status']['error'] = true;
        } else {
            if ($total_num > 1) {
                $user_write_today[$num_tag] = $total_num;
            } else {
                $user_write_today[$num_tag]++;
            }
        }
        $return_array['setWriteToday'] = json_encode($user_write_today);
        return $return_array;
    }

    /**
      +--------------------------------------------------------------------------
     * 获取经验表表名
      +--------------------------------------------------------------------------
     */
    static public function getTableBlog($user_id) {
        return 'tb_blog_' . substr($user_id, -2);
    }

    /**
      +--------------------------------------------------------------------------
     * 获取经验索引表表名
      +--------------------------------------------------------------------------
     */
    static public function getTableBlogIndex() {
        return 'tb_blog_index';
    }

    /**
      +--------------------------------------------------------------------------
     * 获取用户信息表表名
      +--------------------------------------------------------------------------
     * @return string 正式表名
      +--------------------------------------------------------------------------
     */
    static public function getTableUser($user_id) {
        return 'tb_users_' . substr($user_id, -2);
    }
    
    /**
      +--------------------------------------------------------------------------
     * 获取用户信息表表名
      +--------------------------------------------------------------------------
     * @return string 正式表名
      +--------------------------------------------------------------------------
     */
    static public function getTableBaby($user_id) {
        return 'tb_babys_' . substr($user_id, -2);
    }

    /**
      +--------------------------------------------------------------------------
     * 获取用户索引表表名
      +--------------------------------------------------------------------------
     * @return string 正式表名
      +--------------------------------------------------------------------------
     */
    static public function getTableUserIndex() {
        return 'tb_users_index';
    }

    /**
      +--------------------------------------------------------------------------
     * 获取问答表表名
      +--------------------------------------------------------------------------
     */
    static public function getTableWenda($user_id) {
        return 'tb_wenda_' . substr($user_id, -2);
    }

    /**
      +--------------------------------------------------------------------------
     * 获取问答索引表表名
      +--------------------------------------------------------------------------
     */
    static public function getTableWendaIndex() {
        return 'tb_wenda_index';
    }

    /**
      +--------------------------------------------------------------------------
     * 获取照片表表名
      +--------------------------------------------------------------------------
     */
    static public function getTablePhoto($user_id) {
        return 'tb_photo_' . substr($user_id, -2);
    }

    /**
      +--------------------------------------------------------------------------
     * 获取照片索引表表名
      +--------------------------------------------------------------------------
     */
    static public function getTablePhotoIndex() {
        return 'tb_photo_index';
    }

}
